# Maintainer: schuay <jakob.gruber@gmail.com>
# Contributor: Brad Fanella <bradfanella@archlinux.us>
# Contributor: Corrado Primier <bardo@aur.archlinux.org>
# Contributor: danst0 <danst0@west.de>

# Build order: avr-binutils -> avr-gcc -> avr-libc

pkgname=avr-binutils
pkgver=2.40
pkgrel=2
pkgdesc="A set of programs to assemble and manipulate binary and object files for the AVR architecture"
arch=('loong64' 'x86_64')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
depends=('glibc' 'zlib' 'libelf')
options=('!distcc' '!ccache')
source=(https://ftp.gnu.org/gnu/binutils/binutils-${pkgver}.tar.bz2{,.sig}
        avr-size.patch)
sha1sums=('887d24c3799e9fe02646e04e73b5ac2cc3e7188f'
          'SKIP'
          'bb820f761fbbe7379ddf8e75235866c44591c899')
sha256sums=('f8298eb153a4b37d112e945aa5cb2850040bcf26a3ea65b5a715c83afe05e48a'
            'SKIP'
            '7aed303887a8541feba008943d0331dc95dd90a309575f81b7a195650e4cba1e')
validpgpkeys=('3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F') # Nick Clifton (Chief Binutils Maintainer) <nickc@redhat.com>

_builddir=binutils-build

prepare() {
    cd "${srcdir}"/binutils-${pkgver}

    # See https://bugs.archlinux.org/task/32504
    # And https://bugs.archlinux.org/task/40986
    patch -p1 < "${srcdir}"/avr-size.patch

    # https://bugs.archlinux.org/task/34629
    sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

    rm -rf "${srcdir}"/${_builddir}
    mkdir "${srcdir}"/${_builddir}
}

build() {
    cd "${srcdir}"/binutils-${pkgver}

    config_guess=$(./config.guess)

    cd "${srcdir}"/${_builddir}

    "${srcdir}"/binutils-${pkgver}/configure \
        --prefix=/usr \
        --with-lib-path=/usr/lib:/usr/local/lib \
        --with-bugurl=https://bugs.archlinux.org/ \
        --enable-ld=default \
        --enable-gold \
        --enable-plugins \
        --enable-threads \
        --with-pic \
        --disable-werror \
        --disable-multilib \
        --build=${config_guess} \
        --target=avr \
        $CONFIGFLAG

    # This checks the host environment and makes sure all the necessary tools are available to compile Binutils.
    make configure-host

    make tooldir=/usr
}

package() {
    cd "${srcdir}"/${_builddir}

    make prefix="${pkgdir}"/usr tooldir="${pkgdir}"/usr install

    for bin in ar as nm objcopy objdump ranlib strip readelf; do
        rm "${pkgdir}"/usr/bin/${bin}
    done

    rm "$pkgdir"/usr/lib/bfd-plugins/libdep.so

    for info in as bfd binutils gprof ld ctf-spec sframe-spec; do
        mv "${pkgdir}"/usr/share/info/${info}.info "${pkgdir}"/usr/share/info/avr-${info}.info
    done

    rm -r "${pkgdir}"/usr/share/locale
}
